package com.app.voice;

import android.media.MediaPlayer;
import android.media.MediaRecorder;

import com.app.G;
import com.app.util.Utils;
import com.app.util.Utils_Log;

import java.io.File;
import java.io.IOException;

/**
 * Created by matengfei on 15/7/26.
 */
public class MyAudio {

    private String TAG = MyAudio.class.getSimpleName();


    MediaRecorder mMediaRecorder;
    boolean isRecord = false;

    MediaPlayer mPlayer;

    public String filename = "";


    public String init(String filename){ //

        String nowtime = Utils.getNowTime();
        filename = nowtime + "_" + filename;  // 使文件名不存在冲突的现象

        this.filename = G.PATH_AUDIO + "/" + filename;

          /* ①Initial：实例化MediaRecorder对象 */
        mMediaRecorder = new MediaRecorder();

        /* setAudioSource/setVedioSource*/
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置麦克风

        /* 设置输出文件的格式：THREE_GPP/MPEG-4/RAW_AMR/Default
         * THREE_GPP(3gp格式，H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
         */
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);  //amr

         /* 设置音频文件的编码：AAC/AMR_NB/AMR_MB/Default */
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

         /* 设置输出文件的路径 */
        File file = new File(this.filename);
        if (file.exists()) {
            file.delete();
        }
        mMediaRecorder.setOutputFile(file.getAbsolutePath());

        return file.getAbsolutePath();
    }

    public void start_record(){

        if(!isRecord && mMediaRecorder!=null){

            try{
                mMediaRecorder.prepare();
                mMediaRecorder.start();
                // 让录制状态为true
                isRecord = true;
            }catch(IOException ex){
                ex.printStackTrace();
                Utils_Log.e(TAG, "录制失败");
            }

        }


    }

    public String close_record(){

        if (mMediaRecorder != null) {
            isRecord = false;
            mMediaRecorder.release();
            mMediaRecorder = null;
        }

        return filename;

    }


    public byte[] getContent(){

         if(!filename.isEmpty()){

             byte[] bytes = Utils.readFile2Bytes(filename);
             return bytes;

         }

        return new byte[0];

    }



    public void play(){

        if(mPlayer == null)
            mPlayer = new MediaPlayer();

        try{
            mPlayer.setDataSource(filename);
            mPlayer.prepare();
            mPlayer.start();
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    stop_play();
                }
            });
        }catch(IOException e){
            Utils_Log.e(TAG, "播放失败");
        }

    }

    public  void play(String filename, MediaPlayer.OnCompletionListener completionListener){

        if(mPlayer == null)
            mPlayer = new MediaPlayer();

        try{
            mPlayer.reset();
            mPlayer.setDataSource(filename);
            mPlayer.prepare();
            mPlayer.start();
            if(completionListener != null)
                mPlayer.setOnCompletionListener(completionListener);
        }catch(IOException e){
            Utils_Log.e(TAG, "播放失败");
        }

    }

    public void play(String filename){
        play(filename, null);
    }


    public void stop_play(){
        if(mPlayer != null){
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }

    }


    public boolean isPlaying(){
        if(mPlayer == null)
            return false;

        return mPlayer.isPlaying();
    }





}
